<script setup>

import { getUserOrder } from '@/apis/order'
import { ref, onMounted, watch } from 'vue';
import { ElPagination } from 'element-plus';
import 'element-plus/dist/index.css'
// tab列表
const tabTypes = [
  { name: "unpay", label: "未支付" },
  { name: "deliver", label: "已支付" },
  { name: "complete", label: "已完成" },
  { name: "outtime", label: "已超时" },
  { name: "cancel", label: "已取消" },
  { name: "all", label: "全部订单" }
]
// 订单列表
const totalOrders = ref(100); // 替换成实际的订单总数
const ordersPerPage = ref(4); // 每页显示的订单数量
const currentPage = ref(1); // 当前页数 
const orderList = ref([])// 存储所有订单的数组
const showOrders = ref([]); // 显示的订单列表
// const total = ref(0)
const params = ref({
  orderState: 0,
  page: 1,
  pageSize: 4
})
const getOrderList = async () => {
  const res = await getUserOrder(params.value)
  orderList.value = res.data
  // total.value = res.result.counts
  // 从后端获取所有订单数据，假设后端返回的数据为 allOrdersArray
  totalOrders.value = res.data.length
  console.log('订单数量：', res.data.length)
  console.log(orderList.value)
  // 初始化显示的订单列表，默认显示第一页的数据
  showOrders.value = orderList.value.slice(0, ordersPerPage.value);
}


// 监听 currentPage 的变化，更新显示的订单列表
watch(currentPage, (newPage) => {
  const startIndex = (newPage - 1) * ordersPerPage.value;
  const endIndex = newPage * ordersPerPage.value;
  showOrders.value = orderList.value.slice(startIndex, endIndex);

})

onMounted(() => getOrderList())
// tab 切换
const tabChange = (type) => {
  console.log(type)
  params.value.orderState = type
  getOrderList()
}
// 页数切换
// const pageChange = (page) => {
//   console.log(page)
//   params.value.page = page
//   getOrderList()
// }

const handlePageChange = (newPage) => {
  // 处理页数变化，直接从已有的订单数据中进行分页展示
  const startIndex = (newPage - 1) * ordersPerPage.value;
  const endIndex = newPage * ordersPerPage.value;
  showOrders.value = orderList.value.slice(startIndex, endIndex);
  console.log(`Page changed to: ${newPage}`);
};

// 转化日期格式
const formatDate = (date) => {
  return date;
}
// 创建格式化函数
const fomartPayState = (payState) => {
  const stateMap = {
    0: '未支付',
    1: '已支付',
    2: '已完成',
    3: '已超时',
    4: '已取消'
  }
  return stateMap[payState]
}
</script>

<template>
  <div class="order-container">
    <el-tabs @tab-change="tabChange">
      <!-- tab切换 -->
      <el-tab-pane v-for="item in tabTypes" :key="item.name" :label="item.label" />

      <div class="main-container">
        <div class="holder-container" v-if="orderList.length === 0">
          <el-empty description="暂无订单数据" />
        </div>
        <div v-else>
          <!-- 订单列表 -->
          <div class="order-item" v-for="order in showOrders">
            <div class="head">
              <!-- <span>下单时间：{{ order.createTime }}</span>  -->
              <span>订单编号：{{ order.orderId }}</span>

            </div>
            <div class="body">
              <div class="column goods">
                <ul>
                  <li>
                    <div class="info">
                      <p class="date">
                        租车起始时间: {{ formatDate(order.startTime) }}
                      </p>
                      <p class="date">
                        租车结束时间: {{ formatDate(order.endTime) }}
                      </p>
                    </div>
                    <div class="price">订单金额: ￥{{ order.startAmount }}</div>
                    <div class="pre-price" v-if="order.orderStatus == '3'">
                      超时金额：<p class="red">￥ {{ order.timeoutAmount }}</p>
                    </div>
                    <div class="pre-price" v-else>
                      超时金额：<p class="red">￥ 0</p>
                    </div>
                    <div class="pre-date">
                      超时天数:<p class="red" v-if="order.outtime != null">{{ order.outtime }} 天</p>
                      <p class="red" v-else> 0 天</p>
                    </div>
                  </li>
                </ul>
              </div>
              <div class="column state">
                <div class="">
                  <p v-if="order.orderStatus == '3'">
                  <div class="red">
                    {{ fomartPayState(order.orderStatus) }} 3
                  </div>
                  </p>
                  <p v-else-if="order.orderStatus == '2'">
                  <div class="green">
                    {{ fomartPayState(order.orderStatus) }}
                  </div>
                  </p>
                  <p v-else>
                  <div class="">
                    {{ fomartPayState(order.orderStatus) }}
                  </div>
                  </p>
                </div>
              </div>

            </div>
          </div>
          <!-- 分页 -->
          <div class="pagination-container">
            <!-- <el-pagination :total="total" :pageSize="params.pageSize" @current-change="pageChange" background
              layout="prev, pager, next" /> -->
            <!-- 分页组件 -->
            <el-pagination :total="totalOrders" :page-size="ordersPerPage" :pager-count="11" v-model="currentPage"
              @current-change="handlePageChange" background layout="prev, pager, next" />
          </div>
        </div>
      </div>

    </el-tabs>
  </div>
</template>

<style scoped lang="scss">
.order-container {
  padding: 10px 20px;

  .pagination-container {
    display: flex;
    justify-content: center;
  }

  .main-container {
    min-height: 500px;

    .holder-container {
      min-height: 500px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}

.order-item {
  margin-bottom: 20px;
  border: 1px solid #f5f5f5;

  .head {
    height: 50px;
    line-height: 50px;
    background: #f5f5f5;
    padding: 0 20px;
    overflow: hidden;

    span {
      margin-right: 20px;

      &.down-time {
        margin-right: 0;
        float: right;

        i {
          vertical-align: middle;
          margin-right: 3px;
        }

        b {
          vertical-align: middle;
          font-weight: normal;
        }
      }
    }

    .del {
      margin-right: 0;
      float: right;
      color: #999;
    }
  }

  .body {
    display: flex;
    align-items: stretch;

    .column {
      border-left: 1px solid #f5f5f5;
      text-align: center;
      padding: 20px;

      >p {
        padding-top: 10px;
      }

      &:first-child {
        border-left: none;
      }

      &.goods {
        flex: 1;
        padding: 0;
        align-self: center;

        ul {
          li {
            border-bottom: 1px solid #f5f5f5;
            padding: 10px;
            display: flex;

            &:last-child {
              border-bottom: none;
            }

            .info {
              width: 260px;
              text-align: left;
              padding: 0 10px;
              border-right: 1px solid #f5f5f5;

              p {
                margin-bottom: 5px;

                &.date {
                  height: 50px;
                  display: flex;
                  /* 水平居中 */
                  justify-content: center;
                  /* 垂直居中 */
                  align-items: center;
                }

                &.attr {
                  color: #999;
                  font-size: 12px;

                  span {
                    margin-right: 5px;
                  }
                }
              }
            }

            .price {
              width: 200px;
              display: flex;
              /* 水平居中 */
              justify-content: center;
              /* 垂直居中 */
              align-items: center;
              font-size: 16px;
              border-right: 1px solid #f5f5f5;
            }

            .pre-price {
              width: 160px;
              display: flex;
              /* 水平居中 */
              justify-content: center;
              /* 垂直居中 */
              align-items: center;
              font-size: 16px;
              border-right: 1px solid #f5f5f5;
            }

            .red {
              color: $priceColor;
            }

            .pre-date {
              width: 180px;
              display: flex;
              /* 水平居中 */
              justify-content: center;
              /* 垂直居中 */
              align-items: center;
              font-size: 16px;
            }
          }
        }
      }

      &.state {
        width: 120px;
        display: flex;
        /* 水平居中 */
        justify-content: center;
        /* 垂直居中 */
        align-items: center;
        font-size: 16px;

        .green {
          color: $xtxColor;
        }


      }

      &.amount {
        width: 200px;
      }

      &.action {
        width: 140px;

        a {
          display: block;

          &:hover {
            color: $xtxColor;
          }
        }
      }
    }
  }
}
</style>